﻿using System;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace NCMIS.ObjectModel.MetaData
{
    /// <summary>
    /// Represents the relationship object-type definition, which describes a folder object's types attribute values and property defintions.
    /// </summary>
    [DataContract(Name = "cmisTypeRelationshipDefinitionType", Namespace = CmisNs.Cmis)]
    public class TypeRelationshipDefinition : TypeDefinition
    {
        [DataMember(Name = "allowedSourceTypes")]
        public string[] AllowedSourceTypes
        {
            get;
            set;
        }

        [DataMember(Name = "allowedTargetTypes")]
        public string[] AllowedTargetTypes
        {
            get;
            set;
        }

        /// <summary>
        /// Returns a <see cref="System.Xml.Linq.XElement"/> RestAtom representation of this instance.
        /// </summary>
        /// <returns>A <see cref="System.Xml.Linq.XElement"/> object representation.</returns>
        public override XElement ToXElement()
        {
            XElement typeElement = new XElement(CmisNs.XCmisra + "type", new XAttribute(CmisNs.XCmisra + "id", this.Id),
                new XAttribute(XNamespace.Xmlns + "xsi", CmisNs.W3instance),
                new XAttribute(CmisNs.XW3instance + "type", "cmis:cmisTypeRelationshipDefinitionType"),
                new XElement(CmisNs.XCmis + "id", this.Id),
                new XElement(CmisNs.XCmis + "localName", this.LocalName),
                new XElement(CmisNs.XCmis + "localNamespace", this.LocalNamespace),
                new XElement(CmisNs.XCmis + "displayName", this.DisplayName),
                new XElement(CmisNs.XCmis + "queryName", this.QueryName),
                new XElement(CmisNs.XCmis + "description", this.Description),
                new XElement(CmisNs.XCmis + "baseId", BaseIdToString(this.BaseId)),
                new XElement(CmisNs.XCmis + "creatable", this.Creatable),
                new XElement(CmisNs.XCmis + "fileable", this.Fileable),
                new XElement(CmisNs.XCmis + "queryable", this.Queryable),
                new XElement(CmisNs.XCmis + "fullTextIndexed", this.FulltextIndexed),
                new XElement(CmisNs.XCmis + "includedInSupertypeQuery", this.IncludedInSupertypeQuery),
                new XElement(CmisNs.XCmis + "controllablePolicy", this.ControllablePolicy),
                new XElement(CmisNs.XCmis + "controllableACL", this.ControllableAcl));

            if (this.PropertyDefinitions != null)
            {
                foreach (PropertyDefinition propertyDefinition in this.PropertyDefinitions)
                {
                    typeElement.Add(propertyDefinition.ToXElement());
                }
            }

            return typeElement;
        }
    }
}
